package name.koppernickus.lsp.analyzer;

import com.google.common.base.Joiner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClasspathBuilder {

    public static String buildClasspathUsingJARFileNames(String... jarFiles) throws IOException {
        List<String> paths = new ArrayList<String>();
        for (String jarFile : jarFiles) {
            paths.add(findFirstMatchingEntryOnGlobalClasspath(jarFile));

        }
        return Joiner.on(";").join(paths);
    }

    public static String findFirstMatchingEntryOnGlobalClasspath(String jarFileName) {
        String classpath = System.getProperty("java.class.path");
        for (String entry : MetaClassPath.entriesOf(classpath)) {
            if (entry.endsWith(System.getProperty("file.separator") + jarFileName)) {
                return entry;
            }
        }
        throw new RuntimeException("Entry " + jarFileName + " not found on classpath: " + classpath);
    }

}
